package com.example.androidcaculator.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.example.androidcaculator.R;

import java.lang.reflect.Method;

public class ConverterCurrencyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_converter_currency);

        Spinner currencyName1 = findViewById(R.id.currencyName1);
        Spinner currencyName2 = findViewById(R.id.currencyName2);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.items1, android.R.layout.simple_spinner_item);
        currencyName1.setAdapter(adapter);
        adapter = ArrayAdapter.createFromResource(this,R.array.items2, android.R.layout.simple_spinner_item);
        currencyName2.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.currency_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        Intent intent;
        switch (id) {
            case R.id.currency_action_common:
                intent = new Intent(ConverterCurrencyActivity.this, MainActivity.class);
                startActivity(intent);
                break;

            case R.id.currency_action_scientific:
                intent = new Intent(ConverterCurrencyActivity.this,ScientificActivity.class);
                startActivity(intent);
                break;
            case R.id.currency_transform_base:
                intent = new Intent(ConverterCurrencyActivity.this, ConverterBaseActivity.class);
                startActivity(intent);
                break;
            case R.id.currency_transform_length:
                intent = new Intent(ConverterCurrencyActivity.this, ConverterLengthActivity.class);
                startActivity(intent);
                break;
            case R.id.currency_transform_volume:
                intent = new Intent(ConverterCurrencyActivity.this, ConverterVolumeActivity.class);
                startActivity(intent);
                break;
            case R.id.currency_action_dateCal:
                intent = new Intent(this,DateActivity.class);
                startActivity(intent);
                break;
            case R.id.currency_action_help:
                AlertDialog.Builder helpTxt = new AlertDialog.Builder(ConverterCurrencyActivity.this);
                helpTxt.setMessage("这里是帮助文档\n本计算器由Dio开发").setTitle("Help");
                helpTxt.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                helpTxt.show();

                break;

            case R.id.currency_action_exit:
                intent = new Intent(this, MainActivity.class);
                intent.putExtra(MainActivity.EXIST, true);
                startActivity(intent);


        }


        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return super.onMenuOpened(featureId, menu);
    }




}